package tree.tree2.symmetry.leftLeaf;

import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName FindBottomLeftValue
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-05 9:41
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class FindBottomLeftValue {
    /**
     * 输入: root = [2,1,3]
     * 输出: 1
     *
     * @param root
     * @return
     */
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int res = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (i == 0) {
                    res = poll.val;
                }
                if (poll.left != null)
                    queue.add(poll.left);

                if (poll.right != null)
                    queue.add(poll.right);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        FindBottomLeftValue findBottomLeftValue = new FindBottomLeftValue();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        System.out.println(findBottomLeftValue.findBottomLeftValue(root));
    }
}